!// Dmitry.Konovalov@jcu.edu.au Dmitry.A.Konovalov@gmail.com, 2013

! Keep all temp run-time data in this module
module jm_io_mod;
use jm_data_mod;
implicit none;
!private;

contains
! ----- READING FROM FILE

SUBROUTINE jm_input_read(fname);    implicit none;  
  character(LEN=*), intent(in) :: fname;   
  call dbg_str2("-->jm_input_read(fname=", fname); 
  call jm_input_read2(fname, JM_DATA);
  return;
end SUBROUTINE

SUBROUTINE jm_input_read2(fname, self);    implicit none;  
  character(LEN=*), intent(in) :: fname;   
  character(LEN=32) :: help;   
  type(JM_DATA_T), intent(inout) :: self;
  logical ok;
  integer :: iostat_in, nin;
  call dbg_str2("-->jm_input_read2(fname=", fname); 
  
  inquire(file=fname, exist=ok);
  call dbg_str2("inquire(file=fname=", fname);
  call dbg_assert_true("exit if file is missing", ok);
  
  nin = self%INPUT_FILE_UNIT;
  open(nin, file=fname, iostat=iostat_in);

! NOTE: all input is in fixed order
  call read_str(nin, "MODEL_NAME=", self%MODEL_NAME);
  !call jm_input_trgt(nin, self%TRGT);
  !call jm_input_grid(nin, self%GRID);
  !todo more
  close(nin);
  return;
end SUBROUTINE

!SUBROUTINE jm_input_trgt(nin, trgt);    implicit none;  
!  integer, intent(in) :: nin;   
!  type(JM_TRGT_T), intent(out)  :: trgt;
!  call dbg_int("-->jm_input_trgt(nin=", nin);   
!  call read_str(nin, "TRGT_NAME=", trgt%NAME);
!  call read_int(nin, "TRGT_Z=", trgt%Z);
!  return;
!end SUBROUTINE

!SUBROUTINE jm_input_grid(nin, grid);    implicit none;  
!  integer, intent(in) :: nin;   
!  type(JM_GRID_T), intent(out)  :: grid;
!  call dbg_int("-->jm_input_grid(nin=", nin);   
!  call read_int(nin, "NR=", grid%NR);
!  call read_dbl(nin, "MAXR=", grid%MAXR);
!  call read_dbl(nin, "MINR=", grid%MINR);
!  call read_dbl(nin, "LCR1=", grid%LCR1);
!  return;
!end SUBROUTINE
end module